﻿using System.Collections.Generic;

using Ah.Testudin.Engine.Graphics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ah.Testudin.Engine.UI.Elements.Debugging
{
    public class DebugOutputScreenElement : ScreenElementBase
    {
        #region Private Member Variables

        private IList<string> _messages;

        private readonly ITestudinRenderer _renderer;

        private readonly SpriteFont _spriteFont;

        private static DebugOutputScreenElement _instance;

        #endregion

        #region Constructors

        public DebugOutputScreenElement(
            Vector2 position,
            int width,
            int height,
            SpriteFont spriteFont,
            ITestudinRenderer renderer)
            : base(position, width, height)
        {
            _spriteFont = spriteFont;

            _renderer = renderer;

            _messages = new List<string>();
        }

        #endregion

        #region Static Methods

        public static void InitializeSingleton(
            Vector2 position,
            int width,
            int height,
            SpriteFont spriteFont,
            ITestudinRenderer renderer)
        {
            _instance = new DebugOutputScreenElement(
                position,
                width,
                height,
                spriteFont,
                renderer);
        }

        public static DebugOutputScreenElement GetInstance()
        {
            return _instance;
        }

        #endregion

        #region ScreenElementBase

        public override short ZOrder
        {
            get
            {
                return 0;
            }
        }

        public override bool Visible
        {
            get
            {
                return true;
            }
        }

        public override bool ProcessInput()
        {
            return false;
        }

        public override void Update(GameTime gameTime)
        {
        }

        public void AppendMessage(string message)
        {
            _messages.Add(message);
        }

        public override void Render()
        {
            _renderer.BeginSpriteBatch();

            Vector2 currentPosition = _position;

            foreach (string currentMessage in _messages)
            {
                _renderer.RenderString(
                    _spriteFont,
                    currentMessage,
                    currentPosition,
                    Color.Yellow);

                currentPosition.Y += 20;
            }

            _renderer.EndSpriteBatch();

            _messages.Clear();
        }

        public override void Dispose()
        {
            _messages.Clear();
        }

        #endregion
    }
}